﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class ImportBookManager
    {
        public ImportBookInfo GetImportBookById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryImportBook = from d in db.ImportBooks
                                   where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                   select new ImportBookInfo { Id = d.Id, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryImportBook.First();
            }
        
        }


        public IList<ImportBook> ListImportBook()
        {
            List<ImportBook> listdata=new List<ImportBook>();
            using (var db = new QLKDSDataContext())
            {
                listdata = db.ImportBooks.ToList();
                
            }
            return listdata;
        }
    

        public void CreateImportBook(ImportBook ImportBook)
        {
            using (var db = new QLKDSDataContext())
            {
                db.ImportBooks.InsertOnSubmit(ImportBook);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="importBook"></param>
        public void UpdateImportBook(ImportBook importBook)
        {
            using (var db = new QLKDSDataContext())
            {
                var _importBook = db.ImportBooks.Single(d => d.Id.Equals(importBook.Id));
                _importBook.Code = importBook.Code;
                _importBook.ImportDate = importBook.ImportDate;
                _importBook.TotalAmount = importBook.TotalAmount;
                _importBook.SupplierId = importBook.SupplierId;
                _importBook.Description = importBook.Description;
                _importBook.CreatedBy = importBook.CreatedBy;
                _importBook.CreatedDate = importBook.CreatedDate;
                _importBook.ModifiedBy = importBook.ModifiedBy;
                _importBook.ModifiedDate = importBook.ModifiedDate;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteImportBook(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _ImportBook = db.ImportBooks.Single(d => d.Id.Equals(id));

                db.ImportBooks.DeleteOnSubmit(_ImportBook);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasCodeExisted(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.ImportBooks
                            where d.Code.Equals(code)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasCodeDuplicated(string code, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.ImportBooks
                            where d.Code.Equals(code)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
